/**************************************************************************************************
	$Header: /pub/cvsroot/mydns/src/mydns/data.c,v 1.18 2003/03/25 05:41:36 bboy Exp $

	Copyright (C) 2002, 2003  Don Moore <bboy@bboy.net>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at Your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**************************************************************************************************/

#include "named.h"

/* Set this to nonzero for extra debugging in this part of the code */
#define DEBUG_DATA 0


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	FIND_SOA
	Determine the origin in `fqdn' and return the MYDNS_SOA structure for that zone.
	If `label' is non-NULL, the label part of the fqdn will be copied there.
	If provided, `label' should be at least DNS_MAXNAMELEN bytes.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
MYDNS_SOA *
find_soa(
	TASK *t,
	char *fqdn,		/* The FQDN provided; return the SOA for the zone in this FQDN */
	char *label		/* The label part of `fqdn' that is below the origin will be stored here */
)
{
	MYDNS_SOA			*soa = (MYDNS_SOA *)NULL;
	register size_t	fqdnlen = strlen(fqdn);
	register char		*origin, *end;

#if DEBUG_ENABLED && DEBUG_DATA
	Debug("%s: find_soa(\"%s\", \"%s\")", desctask(t), fqdn, label);
#endif

	end = fqdn + fqdnlen;
	for (origin = fqdn; *origin && !soa; origin++)
		if (origin == fqdn || *origin == '.')
		{
			if (*origin == '.' && *(origin+1))
				origin++;

			soa = data_cache_find(0, NULL, DNS_T_SOA, origin, end-origin);

			/* Get label */
			if (soa && label)
			{
				register int origin_len = strlen(soa->origin);
				register int len = strlen(fqdn) - origin_len - 1;

				if (origin_len == 1)
					len++;
				if (len < 0) len = 0;
				if (len > DNS_MAXNAMELEN) len = DNS_MAXNAMELEN;
				memcpy(label, fqdn, len);
				label[len] = '\0';
			}
		}
	return (soa);
}
/*--- find_soa() --------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	FIND_RR
	Look up RR.  Returns NULL if not found.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
MYDNS_RR *
find_rr(
	TASK *t,
	uint32_t zone,			/* The zone to check in */
	char *origin,			/* Origin name to append to 'name' if necessary */
	dns_qtype_t type,		/* The type of record to look for */
	char *name				/* The name to look for in `zone' */
)
{
#if DEBUG_ENABLED && DEBUG_DATA
	Debug("%s: find_rr(%u, \"%s\", %s, \"%s\")",
			desctask(t), zone, origin, mydns_qtype_str(type), name);
#endif
	return (data_cache_find(zone, origin, type, name, strlen(name)));
}
/*--- find_rr() ---------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	FIND_PTR
	Look up PTR record.  Returns NULL if not found.
	There's no cache for PTR records.  In examining a typical DNS server's log files with
	just over a million requests, only 841 (less than 0.1%) were PTR queries.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
MYDNS_PTR *
find_ptr(TASK *t, char *ipstr)
{
	MYDNS_PTR *ptr = (MYDNS_PTR *)NULL;
	struct in_addr addr;

#if DEBUG_ENABLED && DEBUG_DATA
	Debug("%s: find_ptr(\"%s\")", desctask(t), ipstr);
#endif

	addr.s_addr = htonl(mydns_revstr_ip4(ipstr));
	if (mydns_ptr_load(sql, &ptr, &addr) != 0)
		WarnSQL(sql, _("%s: error finding PTR for `%s' (%u)"), desctask(t), ipstr, addr.s_addr);
	return (ptr);
}
/*--- find_ptr() --------------------------------------------------------------------------------*/

/* vi:set ts=3: */
/* NEED_PO */
